﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebFunctions;
using HtmlAgilityPack;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using System.Windows.Forms;

namespace AutoIndustryFiles
{
    class FuJiScraper: FileLinksScraper
    {
        public FuJiScraper()
        {
            this.FileLevel = 3;
            this.MainPage = "http://www.fujielectric.com.cn";
            this.CompanyName = "富士电机(中国有限公司)";
            this.CompanyUrl = "http://www.fujielectric.com.cn/document/product_samples.html";
            this.ColumnName = "首页->资料下载";
            this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);

            WebLink wl = new WebLink();
            wl.LinkLevel = 0;
            wl.LinkId = 0;
            wl.LinkUrl = CompanyUrl;            
            wl.XPath = @"/html/body/div[@id=""contentsArea""]/div[@id=""subContentsBlock""]//dt/a";            
            WebLinks.Add(wl);
        }

        void FuJiScraper_ReviseXPath(ref WebLink wl,int index)
        {
            /*int tableIndex = 0;
            if (wl.LinkLevel == 2)
            {                
                if (wl.LinkUrl.IndexOf("product_manual") < 0)
                {
                    tableIndex = int.Parse(wl.LinkUrl.Substring(wl.LinkUrl.Length - 1, 1)) + 1;
                    wl.XPath = wl.XPath.Replace("table", "table[" + tableIndex.ToString() + "]");
                }
                else
                    wl.XPath = wl.XPath.Replace("table", "table[" + (index+1).ToString() + "]");
            }*/
            wl.XPath = wl.XPath.Replace("table", "table[" + (index + 1).ToString() + "]");
        }

        protected bool UpdateAndCancelled(string msg)
        {
            bool bCancelled = false;
            UpdateMsg("FujiScraper", msg,ref bCancelled);

            Application.DoEvents();

            return bCancelled;
        }

        public override void GenerateAllLinks()
        {
            if (UpdateAndCancelled("正在获取 " + WebLinks[0].LinkUrl))
                return;

            HtmlDoc doc = new HtmlDoc();            
            doc.LoadHtml(WebFuncs.OpenUrlEx(WebLinks[0].LinkUrl,"UTF-8"));

            WebLinks.Add(WebLinks[0].NewSubLink(WebLinks.Count, "http://www.fujielectric.com.cn/document/product_samples.html",
                "产品样本",
                "",
                @"html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[3]//table/thead/tr[1]/th"));//@"/html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[2]//li/a"));

            WebLinks.Add(WebLinks[0].NewSubLink(WebLinks.Count, "http://www.fujielectric.com.cn/document/product_manual.html",
                "使用说明书",
                "",
                @"html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[3]//table/thead/tr[1]/th"));//@"/html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[2]/table//td/a"));

            WebLinks.Add(WebLinks[0].NewSubLink(WebLinks.Count, "http://www.fujielectric.com.cn/document/product_specification.html",
                "产品规格书",
                "",
                @"html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[3]//table/thead/tr[1]/th"));//@"/html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[2]//a"));

            ///@"html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[3]//table/thead/tr[1]/th"));//
            //第2层与第3层在同一页面
            int k = WebLinks.Count;

            for (int i = 1; i < k; i++)
            {
                int k1 = WebLinks.Count;

                if (UpdateAndCancelled("正在获取 " + WebLinks[i].LinkUrl))
                    return;
                doc.LoadHtml(WebFuncs.OpenUrlEx(WebLinks[i].LinkUrl, "UTF-8"));                

                if (i == 1)
                    GenerateNextLevelLinks(doc, WebLinks[i],@"/html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[3]//table/tbody/tr","td[1]","td[2]/a");
                else if (i == 2)
                    GenerateNextLevelLinks(doc, WebLinks[i], @"/html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[3]//table/tbody/tr", "td[2]", "td[3]/a");
                else
                    GenerateNextLevelLinks(doc, WebLinks[i], @"/html/body/div[@id=""contentsArea""]/div[1]/div[1]/div[3]//table/tbody/tr", "td[2]", "td[3]/a");

                int k2 = WebLinks.Count;
                //k1至k2-1为新增的weblink
                for (int j = k1; j < k2; j++)
                {
                    GenerateNextLevelLinks(doc, WebLinks[j], "", "", "", "");
                }
            }
            UpdateAndCancelled("已完成");
        }
    }
}
